package com.bawei.week.visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.entity.VisitStatusBean
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.week.R
import com.bawei.week.adapter.VisitAdapter
import com.bawei.week.adapter.VisitStatusAdapter
import com.bawei.week.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding,VisitViewModel>() {
    val visitAdapter by lazy { VisitAdapter() }
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    var status = "全部状态"
    override fun initView() {
        val arrays = mutableListOf<VisitStatusBean>(
            VisitStatusBean("全部状态", true), VisitStatusBean("待审核",false),
            VisitStatusBean("已通过", false), VisitStatusBean("已完成",false),
            VisitStatusBean("驳回", false)
        )
        visitStatusAdapter.addData(arrays)

        binding.rvVisitStatus.apply {
            adapter = visitStatusAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity,LinearLayoutManager.HORIZONTAL,false)
        }

        binding.rvVisit.apply {
            adapter = visitAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity)
        }

        //点击进入添加预约来访
        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/VisitAddActivity").navigation()
        }

        //点击进入预防来访详情页
        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/VisitDetailActivity").withInt("visitId",visitAdapter.data[position].id).navigation()
        }

        //点击不同状态切换列表
        visitStatusAdapter.setOnItemClickListener{ adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isChecked = false
            }
            visitStatusAdapter.data[position].isChecked = true

            status = visitStatusAdapter.data[position].status
            if (status == "全部状态" || status == "已完成"){
                vm.getVisit()
            }else{
                vm.getVisitStatus(visitStatusAdapter.data[position].status)
            }
        }

        //根据姓名搜索列别
        binding.visitSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.getVisitName(query!!)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })
    }

    override fun initData() {
        vm.visitSuc.observe(this){
            visitAdapter.data.clear()
            if (status == "已完成"){
                visitAdapter.addData(it.filter {
                    it.visitStatus == "已通过" || it.visitStatus == "驳回"
                })
            }else{
                visitAdapter.addData(it.sortedByDescending {
                    it.id
                })
            }
            visitAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getVisit()
    }
}